﻿using System;
using System.Collections;
using System.Drawing;
using System.Reflection;
using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslDesign = global::Microsoft.VisualStudio.Modeling.Design;
using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
using DslDefinition = global::Microsoft.VisualStudio.Modeling.DslDefinition;

namespace Salamanca.Sdml
{
    partial class DomainEntityShape
    {
        // Naive attempt to paint the shapes individually according to their InheritanceModifier property
        // cf. http://social.msdn.microsoft.com/forums/en-US/vsx/thread/8dbfed93-6cb5-4244-a189-d4fd49ec5b99/

        //protected override void InitializeInstanceResources()
        //{
        //    base.InitializeInstanceResources();

        //    DomainEntityShape.AssociateValueWith(Store, DomainEntity.InheritanceModifierDomainPropertyId);
        //}

        //protected override void OnAssociatedPropertyChanged(DslDiagrams.PropertyChangedEventArgs e)
        //{
        //    if ("InheritanceModifier".Equals(e.PropertyName))
        //    {
        //        DslDiagrams.PenSettings s=null;

        //        switch ((InheritanceModifier)e.NewValue)
        //        {
        //        case InheritanceModifier.Abstract:
        //            //((DslDefinition.TextDecorator)ModelElement).FontStyle=FontStyle.Bold|FontStyle.Italic;
        //            s=StyleSet.GetOverriddenPenSettings(OutlinePenId);
        //            s.Width=0.01F;
        //            StyleSet.OverridePen(OutlinePenId, s);
        //            break;
        //        case InheritanceModifier.None:
        //            //((DslDefinition.TextDecorator)ModelElement).FontStyle=FontStyle.Bold;
        //            s=StyleSet.GetOverriddenPenSettings(OutlinePenId);
        //            s.Width=0.01F;
        //            StyleSet.OverridePen(OutlinePenId, s);
        //            break;
        //        case InheritanceModifier.Sealed:
        //            //((DslDefinition.TextDecorator)ModelElement).FontStyle=FontStyle.Bold;
        //            s=StyleSet.GetOverriddenPenSettings(OutlinePenId);
        //            s.Width=0.03F;
        //            StyleSet.OverridePen(OutlinePenId, s);
        //            break;
        //        }
        //    }

        //    base.OnAssociatedPropertyChanged(e);
        //}

        protected override void InitializeFromMappings()
        {
            base.InitializeFromMappings();

            if (!compartmentMappings.ContainsKey(GetType()))
            {
                DslDiagrams::CompartmentMapping attributesMapping=Array.Find<DslDiagrams::CompartmentMapping>(
                    GetCompartmentMappings(),
                    delegate(DslDiagrams::CompartmentMapping mapping) {
                        DslDiagrams::ElementListCompartmentMapping m=mapping as DslDiagrams::ElementListCompartmentMapping;
                        if (m==null)
                            return false;

                        return m.CompartmentId=="AttributesCompartment";
                    }
                );
                if (attributesMapping!=null)
                {
                    DslDiagrams::ElementListCompartmentMapping mapping=(DslDiagrams::ElementListCompartmentMapping)attributesMapping;
                    mapping.ImageGetter=new DslDiagrams.DisplayImageGetter(_AttributesCompartmentImageProvider);
                }
            }
        }

        private Image _AttributesCompartmentImageProvider(DslModeling.ModelElement element)
        {
            Image attr=Image
                .FromStream(Assembly
                .GetExecutingAssembly()
                .GetManifestResourceStream("Salamanca.Sdml.Resources.EntityAttribute.bmp"));
            Bitmap ret=new Bitmap(attr.Width, attr.Height);

            using (Graphics g=Graphics.FromImage(ret))
            {
                g.DrawImage(attr, Point.Empty);

                EntityAttribute attribute=element as EntityAttribute;
                if (attribute!=null)
                {
                    if (attribute.Unique)
                    {
                        Bitmap unique=(Bitmap)Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Salamanca.Sdml.Resources.EntityAttributeUniqueOverlay.bmp"));
                        unique.MakeTransparent();
                        g.DrawImage(unique, Point.Empty);
                    }

                    if (!attribute.AllowNull)
                    {
                        Bitmap notNull=(Bitmap)Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Salamanca.Sdml.Resources.EntityAttributeNotNullOverlay.bmp"));
                        notNull.MakeTransparent();
                        g.DrawImage(notNull, Point.Empty);
                    }
                }
            }

            return ret;
        }
    }
}
